﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Services;
using System.Data.Services.Client;
using System.Data.Services.Common;   
using OrmChat;
using System.Web.Security;
using System.Runtime.Serialization.Json;
using ChatModel;

public partial class Controls_SilverChatControl : System.Web.UI.UserControl
{
		Chat mChat = new Chat();
		protected void Page_Load(object sender, EventArgs e)
		{
				//if (!Page.User.Identity.IsAuthenticated)
						//Server.Transfer(FormsAuthentication.LoginUrl);

				if (!Page.IsPostBack)
				{
						mChat.m_UserName = Page.User.Identity.Name;
						mChat.m_UserIdStr = mChat.getUtenteID(mChat.m_UserName).ToString() ;

						mChat.pathJS = getPrimaryDomainUrl() + "/JavaScript/myChat.js";

						//json.WriteObject(  
						//getChats();
				}
		}//f(

		public string getPrimaryDomainUrl()
		{				
						string[] sp = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.Split('/');
						return (sp[0] + "//" + sp[2]);
				
		}//fm

	
		private ChatRooms[] getChats()
		{
				try
				{
						return mChat.getChats();

						//foreach (var item in dati)
						//{
						//  m_Chats += " &nbsp;<input id='Radio" + item.ChatRoomID + "' name='gruppone' type='radio'  title='" +
						//   item.Name + "'  value='" + item.ChatRoomID.ToString() +
						//   "' onclick=" + "\"" + "javascript:setIdChat(" + item.ChatRoomID + ",'" + item.Name + "');" + "\"" + "/>" + item.Name + " &nbsp;";
						//}
				}
				catch (Exception ex)
				{
						throw;
				}

		}//fm


		[System.Web.Services.WebMethod]
		public static string getIndiceMessaggio(int chatId)
		{
				try
				{
						int id = new Chat().getIndiceMessaggio(chatId);
						return id.ToString();

						//cancella
						//db.ExecuteCommand("DELETE FROM CHATMESSAGE WHERE chatRoomId={0}", chatId );
						//"yyyy-MM-dd dd:mm:ss.mm")
						//CultureInfo gCulture = new CultureInfo("en-US");
						//string f="yyyy-MM-dd hh:mm:ss";
						//return (dati == null ? DateTime.Now.ToString( f,gCulture) : dati.Value.ToString(f, gCulture));

				}
				catch (Exception ex)
				{
						return "1";
						throw new Exception("Errore getIndiceMessaggio() " + ex.Message);
				}
		}//fm

		[System.Web.Services.WebMethod]
		public static ChatLoggedInUsers[] startChat(int chatId, string userId, string userName)
		{
				try
				{
						var utenti = new Chat().startChat(chatId, userId, userName);
						return utenti;

				}
				catch (Exception ex)
				{
						throw new Exception("Errore startChat() " + ex.Message + "\n" + ex.StackTrace);
				}

		}//fm


		[System.Web.Services.WebMethod]
		public static ChatLoggedInUsers[] getUtentiConnessi(int chatId)
		{
				try
				{
						var utentiConnessi = new Chat().getUtentiConnessi(chatId);
						return utentiConnessi;
				}
				catch (Exception ex)
				{
						throw new Exception("Errore getUtentiConnessi() " + ex.Message);
				}

		}//fm


		[System.Web.Services.WebMethod]
		public static void terminaChat(int chatId, string userName)
		{
				termina(chatId, userName);

		}//fm

		[System.Web.Services.WebMethod]
		public static bool deleteObsoleti()
		{
				try
				{
						 return new Chat().deleteObsoleti();
				}
				catch
				{ throw; }

		}//fm

		private static void termina(int chatId, string userName)
		{
				try
				{
					new Chat().terminaChat(chatId,userName);   
				}
				catch (Exception ex)
				{
						throw new Exception("Errore in termina() : " + ex.Message);
				}
		}//fm

	
		[System.Web.Services.WebMethod]
		public static string prova(string aa, string bb)
		{
				return aa + " .. " + bb;
		}

		[System.Web.Services.WebMethod]
		public static myChatMex[] GetMessaggi(int chatId, int lastMsgId)
		{		  
     var mess = new Chat().prelevaMessaggi(chatId,lastMsgId);  
						return mess;
		}//fm

		[System.Web.Services.WebMethod]
		public static bool SetMessaggi(string msg, string chatId, string userid, string userName, string style)
		{

				try
				{
				  return new Chat().SetMessaggi(msg,int.Parse(chatId),int.Parse(userid),userName,style);  
				}
				catch (Exception ex)
				{
						throw new Exception("Errore in SetMessaggi() : " + ex.Message);
				}

		}//fm

}//fC
